package com.ssm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageSerializable;
import com.ssm.entity.Classes;
import com.ssm.entity.Student;
import com.ssm.mapper.ClassesMapper;
import com.ssm.mapper.StudentMapper;
import com.ssm.service.StudentService;
import com.ssm.util.PageResult;
import jdk.internal.dynalink.support.ClassMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;

    @Override
    public List<Student> findAll() {
        List<Student> students = studentMapper.selectByExample(null);
        students.forEach(st -> {
            Classes classes = classesMapper.selectByPrimaryKey(st.getCid());
            st.setCname(classes.getCname());
        });
        return students;
    }


    //分页查询
    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize) {
        //1.开始分页
        PageHelper.startPage(page,pageSize);
        //2.开始查询
        List<Student> students = findAll();
        //3.将查询得到的数据转换成Page对象
        Page<Student> studentPage = (Page<Student>) students;
        //4.转换为PageResult对象
        return new PageResult<>(studentPage.getResult(),studentPage.getTotal());
    }
}


